package com.rxt.thermopro.bean

import com.rxt.ble.data.BleDevice

data class DeviceInfoBean(
    var name: String = "",
    var address: String = "",
    val bleDevice: BleDevice? = null,
    var infoItemList: MutableList<InfoItemBean> = mutableListOf(),
    var tempUnit: String = "",
    var deviceType: String = "",
    var version: String = "",
    var electricity: String = ""
) {
    /**
     * aabb0fff0fff0fff0fff0fff0fff0fff0fff0000312e312e30051eff
     */
    fun formatInfo(infoMsg: String): DeviceInfoBean {
        val infoItemBean1 = InfoItemBean()
        val highNum1 = infoMsg.substring(4, 8)
//        if (highNum1.startsWith("00")){
//            highNum1=highNum1.substring(2)
//        }
        val lowNum1 = infoMsg.substring(8, 12)
        infoItemBean1.highTemperatureNUm =
            if (highNum1 == "0fff") "" else Integer.parseInt(highNum1, 16).toString()
        infoItemBean1.lowTemperatureNUm = if (lowNum1 == "0fff" || lowNum1=="ffff") "" else Integer.parseInt(lowNum1, 16).toString()
        infoItemBean1.singleTemp=lowNum1=="ffff"

        val infoItemBean2 = InfoItemBean()
        val highNum2 = infoMsg.substring(12, 16)
        val lowNum2 = infoMsg.substring(16, 20)
        infoItemBean2.highTemperatureNUm =
            if (highNum2 == "0fff") "" else Integer.parseInt(highNum2, 16).toString()
        infoItemBean2.lowTemperatureNUm = if (lowNum2 == "0fff" || lowNum2=="ffff") "" else Integer.parseInt(lowNum2, 16).toString()
        infoItemBean2.singleTemp=lowNum2=="ffff"

        val infoItemBean3 = InfoItemBean()
        val highNum3 = infoMsg.substring(20, 24)
        val lowNum3 = infoMsg.substring(24, 28)
        infoItemBean3.highTemperatureNUm =
            if (highNum3 == "0fff") "" else Integer.parseInt(highNum3, 16).toString()
        infoItemBean3.lowTemperatureNUm = if (lowNum3 == "0fff" || lowNum3=="ffff") "" else Integer.parseInt(lowNum3, 16).toString()
        infoItemBean3.singleTemp=lowNum3=="ffff"

        val infoItemBean4 = InfoItemBean()
        val highNum4 = infoMsg.substring(28, 32)
        val lowNum4 = infoMsg.substring(32, 36)
        infoItemBean4.highTemperatureNUm =
            if (highNum4 == "0fff") "" else Integer.parseInt(highNum4, 16).toString()
        infoItemBean4.lowTemperatureNUm = if (lowNum4 == "0fff" || lowNum4=="ffff") "" else Integer.parseInt(lowNum4, 16).toString()
        infoItemBean4.singleTemp=lowNum4=="ffff"

        infoItemList.add(infoItemBean1)
        infoItemList.add(infoItemBean2)
        infoItemList.add(infoItemBean3)
        infoItemList.add(infoItemBean4)

        val electricityStr=infoMsg.substring(infoMsg.length-6, infoMsg.length-4)
        println("=====>electricityStr=$electricityStr")
        electricity=Integer.parseInt(electricityStr, 16).toString()
        return this
    }
}